
enum RXPIN {
    //% block="GPIO8"
    8
}
enum TXPIN {
    //% block="GPIO7"
    7
}

enum baudRate {
    //% block="9600"
    9600,
    //% block="4800"
    4800,
    //% block="115200"
    115200
}

enum BTN {
    //% block="A"
    A,
    //% block="B"
    B,
    //% block="A+B"
    AB
}

//% color="#1e1e1e" iconWidth=50 iconHeight=40
namespace ESP32_TO_UART {
    //% block="初始网络化模块 设置引脚  RX[RXPIN] TX[TXPIN] 波特率[baudRate]"  blockType="command"
    //% TXPIN.shadow="dropdown" TXPIN.options="TXPIN" TXPIN.defl="GPIO8"
    //% RXPIN.shadow="dropdown" RXPIN.options="RXPIN" RXPIN.defl="GPIO7"
    //% baudRate.shadow="dropdown" baudRate.options="baudRate" baudRate.defl="baudRate.9600"
    export function ESP32_TO_UART_init(parameter: any, block: any) {
        let rx = parameter.RXPIN.code;
        let tx = parameter.TXPIN.code;
        let baudRate = parameter.baudRate.code;
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addInclude('splicingStrings', '#include <splicingStrings.h>');
        Generator.addObject(`ESP32_TO_UART_OBJ`, `SoftwareSerial`, `mySerial(${rx},${tx});`);
        Generator.addSetup(`mySerial.begin`, `mySerial.begin(${baudRate});`);
    }

    //% block="发送消息   [STR]"  blockType="command"
    //% STR.shadow="string" STR.defl="Hello"
    export function ESP32_TO_UART_SEND(parameter: any, block: any) {
        let str = parameter.STR.code;
        Generator.addCode(`mySerial.print(${str});`);
    } 

    //% block="有未读消息?"  blockType="boolean"
    export function ESP32_TO_UART_available(parameter: any, block: any) {
        Generator.addCode("mySerial.available()", Generator.ORDER_UNARY_POSTFIX);
    }

    //% block="读取网络模块消息"  blockType="reporter"
    export function ESP32_TO_UART_READ(parameter: any, block: any) {
        Generator.addCode(`getString(mySerial.read())`, Generator.ORDER_UNARY_POSTFIX);
    }
}
